Skip to content

perf(document): avoid enumerating Buffer bytes and large values when skipping document array element paths - #16766

Open
harpreetkbadhan wants to merge 1 commit into
Automattic:masterfrom
harpreetkbadhan:fix/validate-buffer-in-doc-array
Open

harpreetkbadhan wants to merge 1 commit into
Automattic:masterfrom
harpreetkbadhan:fix/validate-buffer-in-doc-array

Conversation

@harpreetkbadhan

Copy link
Copy Markdown

Summary

Fixes #16765.

Since 9.11.0 (#16484), validating a document whose document array elements contain a Buffer, a large primitive array or a Mixed object with many keys costs time and memory in proportion to the size of that value. _getPathsToValidate() called subdoc.modifiedPaths({ includeChildren: true }) for each element. That expands every modified object value with Object.keys(), which gives one path per byte for a Buffer. A few MB of buffer data in a document array element can run the process out of heap memory during save() / validate().

This PR replaces that loop with _deleteDocumentArrayElementDescendants(). When the document array is validated as a whole, the helper scans the paths set once and removes every entry under one of the array's elements. The paths set only holds paths that are actually up for validation, so the cost follows the schema rather than the data. Scanning once per array, rather than once per element, also keeps the cost linear for large arrays. The #16484 behaviour is unchanged: element subpaths, including nested document array paths, are still dropped when the parent array covers them, and the existing tests for it pass.

Repro from the issue, before → after:

1 MB buffer -> validate() 272 ms  → 1 ms
4 MB buffer -> validate() 1656 ms → 0 ms
8 MB buffer, --max-old-space-size=256: heap OOM → 0 ms

Examples

Tests added in test/document.validate.test.js under large values in document array elements (gh-16765). A 4 MB Buffer, a 100k-element [Number] and a 100k-key Mixed object inside a document array element validate without modifiedPaths() returning per-byte, per-element or per-key paths. There's also a check that element-level required errors next to a large Buffer are still reported.

…skipping document array element paths

Re: Automattic#16765

_getPathsToValidate() called subdoc.modifiedPaths({ includeChildren: true })
for every document array element, which expands Buffers byte by byte and
large primitive arrays / Mixed objects key by key. Instead, scan the paths
to validate once per array and drop the ones under covered elements, so the
cost follows the schema rather than the data. Keeps the Automatticgh-16484 behaviour.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

validate() enumerates every byte of a Buffer inside a document array element, running out of memory since 9.11.0

1 participant